package com.yanqu.road.server.cmd.scene;

import com.yanqu.road.entity.enums.eSceneActionType;
import com.yanqu.road.entity.scene.UserScene;
import com.yanqu.road.logic.pb.ScenePb;
import com.yanqu.road.pb.scene.SceneProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.scene.CrossSceneMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;

@Cmd(code = CrossProtocol.C2_CROSS_SCENE_UPDATE, desc = "跨服修改场景状态")
public class CrossUpdateSceneCmd extends CrossCommand {

    private int activityId;
    private int type;
    private UserScene userScene;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SceneProto.CrossUpdateSceneReqMsg reqMsg = SceneProto.CrossUpdateSceneReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        userScene = ScenePb.parseUserScene(reqMsg.getSceneMsg());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SceneProto.SceneActionMsg.Builder actionBuilder = SceneProto.SceneActionMsg.newBuilder()
                .setPlayerSceneInfo(ScenePb.parsePlayerSceneMsg(userScene.getPlayerSceneInfo()))
                .setSceneType(userScene.getSceneType())
                .setSceneId(userScene.getSceneId())
                .setActionType(eSceneActionType.EnterScene.getValue());
        if (type == 1) {
            CrossSceneMgr.enterScene(userScene, false);
            actionBuilder.setActionType(eSceneActionType.EnterScene.getValue());
            CrossSceneMgr.addAction(activityId, actionBuilder.build());
        } else if (type == 2) {
            CrossSceneMgr.quitScene(userScene.getUserId(), userScene.getUpdateTime());
            actionBuilder.setActionType(eSceneActionType.QuitScene.getValue());
            CrossSceneMgr.addAction(activityId, actionBuilder.build());
        }

    }

    @Override
    public void sendData() {

    }
}
